package gwtappcontainer.client.apps.home.main;

import gwtappcontainer.client.ContainerUtil;
import gwtappcontainer.client.apps.IPresenter;
import gwtappcontainer.client.apps.home.HomeRpcService;
import gwtappcontainer.client.apps.home.HomeRpcServiceAsync;

import java.util.Map;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;

public class Presenter implements IPresenter {	
	
	private Logger logger;
	
	public Presenter() {
		logger = ContainerUtil.getLogger(Presenter.class.getName());
	}
	 
	@Override
	public void render(FlowPanel fp, Map<String, String> params) {
		final View view = new View();
		
		fp.clear();
		
		HomeRpcServiceAsync rpcSvc = GWT.create(HomeRpcService.class);
		
		rpcSvc.getAllAppsHtml(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				String errMessage = "Exception thrown by rpc." + caught.getMessage();
				logger.severe(errMessage + "\n" + caught.toString());			
				Window.alert(errMessage + 
						". Try reloading the page by clicking the refresh button in your browser");
				//to do : change to error dialog box					
			}

			@Override
			public void onSuccess(String result) {
				if (result != null) 
					view.allAppsHtml.setHTML(result);				
			}
		});	
				
		
		fp.add(view);		
	}
}
